// pages/manage/manageFoodList.ts
import {
  getFoodList,
  findCanteenNameById,
  addSiteIntofoodList
} from "../../apis/apis"
var app = getApp();
var apiUrl = getApp().globalData.apiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canteenName: "饭堂",
    canteenId: 0,
    pickerIndex: 0,
    classArr: [],
    array: [],
    foodListArr: [],
    foodPageNo: 1,
    canteenArr: ["一饭一楼", "一饭三楼", "二饭", "三饭"],
    canteenPickerIndex: 0,
    disPicker: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    wx.setNavigationBarTitle({
      title: '悦食生活-菜品管理',
    })
    await this.setData({
      canteenId: parseInt(options.canteenId),
      canteenName: findCanteenNameById(options.canteenId,app.globalData.canteenList)
    })
    if (app.globalData.userInfo.permission == 2) {
      this.setData({
        disPicker: false,
        canteenPickerIndex:parseInt(options.canteenId)-1
      })
    } else {
      this.setData({
        canteenPickerIndex:app.globalData.userInfo.permission - 3
      })
    }
  await this.onShow();
  },

  pickerChange: function (e) {
    this.setData({
      pickerIndex: e.detail.value,
      foodPageNo:1
    })
    getFoodList(this.data.foodPageNo, 10, this.data.canteenId, this.data.classArr[e.detail.value].id).then(res => {
      this.setData({
        foodListArr: res.rows
      })
    })
  },
  canteenPickerChange:function(e){
    this.setData({
      canteenPickerIndex:e.detail.value,
      canteenName:this.data.canteenArr[this.data.pickerIndex],
      canteenId:parseInt(e.detail.value)+1
    })
    this.onShow();
  },
  addFoodButton() {
    wx.navigateTo({
      url: 'manageFoodAdd?canteenId='+this.data.canteenId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.request({
      url: apiUrl + '/food-class/getFoodClassListById',
      method: 'POST',
      data: {
        'canteenId': parseInt(this.data.canteenId)
      },
      success: res => {
        console.log(res);
        let arrayTemp = [""];
        for (let i = 0; i < res.data.data.data.length; i++) {
          arrayTemp[i] = res.data.data.data[i].foodClassName
        }
        this.setData({
          array: arrayTemp,
          classArr: res.data.data.data
        });
        wx.request({
          url: apiUrl + '/food/getFoodList',
          method: 'POST',
          data: {
            "pageNo": this.data.foodPageNo,
            "pageSize": 10,
            "canteenId": this.data.canteenId,
            "foodClassId": this.data.classArr[this.data.pickerIndex].id
          },
          success: inRes => {
            this.setData({
              foodListArr: inRes.data.data.rows
            })
            console.log(this.data.foodListArr);
          }
        })
      }
    })
  },
  reload(){
    
    wx.request({
      url: apiUrl + '/food-class/getFoodClassListById',
      method: 'POST',
      data: {
        'canteenId': this.data.canteenId
      },
      success: res => {
        console.log(res);
        let arrayTemp = [""];
        for (let i = 0; i < res.data.data.data.length; i++) {
          arrayTemp[i] = res.data.data.data[i].foodClassName
        }
        this.setData({
          array: arrayTemp,
          classArr: res.data.data.data
        });
        wx.request({
          url: apiUrl + '/food/getFoodList',
          method: 'POST',
          data: {
            "pageNo": this.data.foodPageNo,
            "pageSize": 10,
            "canteenId": this.data.canteenId,
            "foodClassId": this.data.classArr[this.data.pickerIndex].id
          },
          success: inRes => {
            this.setData({
              foodListArr: inRes.data.data.rows
            })
            console.log(this.data.foodListArr);
          }
        })
      }
    })
  },
  continueLoad() {
    this.setData({
      foodPageNo: this.data.foodPageNo + 1
    })
    getFoodList(this.data.foodPageNo, 10, this.data.canteenId, this.data.classArr[this.data.pickerIndex].id).then(res => {
      console.log(res);
      addSiteIntofoodList(this.data.currentCanteenName, res.rows)
      let oldFoodArr = this.data.foodListArr
      let newFoodArr = oldFoodArr.concat(res.rows)
      this.setData({
        foodListArr: newFoodArr
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})